﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy2_13_2_
{
    public partial class Form1 : Form
    {
        private string specialChar = "~!@#$%^&*()_+|{}<>?-=[]\\\";',./";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private int CheckPass(string pass)
        {
            // 1. 检查长度
            if (pass.Length < 8) return 0;

            // 2. 检查字符类型（cType[0]=数字, 1=大写, 2=小写, 3=特殊字符）
            int[] cType = new int[4];
            foreach (char c in pass)
            {
                if (char.IsNumber(c))
                    cType[0] = 1;
                else if (char.IsUpper(c))
                    cType[1] = 1;
                else if (char.IsLower(c))
                    cType[2] = 1;
                else if (specialChar.IndexOf(c) != -1)
                    cType[3] = 1;
                else
                    return 0; // 包含非法字符
            }

            // 3. 统计包含的字符类别数
            int typeCount = cType[0] + cType[1] + cType[2] + cType[3];
            return typeCount;
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            string password = txtPassword.Text;
            int typeCount = CheckPass(password);

            // 重置强度标签样式
            ResetLabels();

            switch (typeCount)
            {
                case 0:
                    MessageBox.Show("密码不得小于8位！\n且只能是字母、数字和以下特殊字符的组合：\n" + specialChar);
                    break;
                case 1:
                    lblWeak.BackColor = Color.Red;
                    lblWeak.Text = "弱";
                    break;
                case 2:
                    lblWeak.BackColor = Color.Red;
                    lblWeaker.BackColor = Color.Orange;
                    lblWeaker.Text = "较弱";
                    break;
                case 3:
                    lblWeak.BackColor = Color.Red;
                    lblWeaker.BackColor = Color.Orange;
                    lblStronger.BackColor = Color.Yellow;
                    lblStronger.Text = "较强";
                    break;
                case 4:
                    lblWeak.BackColor = Color.Red;
                    lblWeaker.BackColor = Color.Orange;
                    lblStronger.BackColor = Color.Yellow;
                    lblStrong.BackColor = Color.Green;
                    lblStrong.Text = "强";
                    break;
            }
        }

        // 重置强度标签
        private void ResetLabels()
        {
            lblWeak.BackColor = Color.LightGray;
            lblWeaker.BackColor = Color.LightGray;
            lblStronger.BackColor = Color.LightGray;
            lblStrong.BackColor = Color.LightGray;
            lblWeak.Text = "";
            lblWeaker.Text = "";
            lblStronger.Text = "";
            lblStrong.Text = "";
        }

    }
}
